<template>
  <div class="register">
      <el-form 
        :model="ruleForm" 
        status-icon 
        :rules="rules" 
        ref="ruleForm" 
        class="register-box">

        <div class="title">用户登陆</div>

        <el-form-item prop="phone">
          <el-input type="text" v-model="ruleForm.phone" autocomplete="off" placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item class="form-footer">
          <el-button type="primary" @click="submitForm()">登陆</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import {user_login} from '../utils/api'

export default {
  data(){
    var validatePhone= (rule, value, callback) => {
      if ( !/^1[3-9]\d{9}$/.test(value) ) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if ( !/^\d{6}$/.test(value) ) {
        callback(new Error('请输入正确的密码'));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        phone: '', //手机号
        pass: '', //密码
      },
      rules: {
        phone: [
          //validator 验证器 , trigger 触发器
          { validator: validatePhone, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },

  methods:{
    submitForm(){
      //获取Form组件的实例, 调用实例的方法( validate内部会依次调用每一个 表单项的验证器 实现表单验证 )
      this.$refs.ruleForm.validate((valid)=>{
        if( valid ){

          //发起登陆请求.
          user_login( this.ruleForm ).then((res)=>{
            console.log(res.data);
            if( res.data.code == 200 ){
              
              this.$message({ message:res.data.msg,type:'success' });
              //保存token
              localStorage.setItem('token',res.data.token);
              //保存userinfo
              //localStorage.setItem('userinfo',JSON.stringify( res.data.userinfo ));
              this.$store.commit('save_userinfo',res.data.userinfo);
        
              //过滤路由表, 过滤并生成该用户有权限访问的路由
              //将最终生成的 该用户有权限访问的路由 动态添加到公共路由表中.
              this.$router.addRoutes( this.$store.getters.getRoutes );
              //console.log( this.$store.getters.getRoutes  );
               //跳转到首页

              this.$router.push('/index');
            }else{
              this.$message({ message:res.data.msg,type:"error" });
            }
          })
          
        }else{
          console.log('验证失败');

        }
      })
    }
  }
}
</script>

<style lang='scss'>
.register{
  height: 100%;
  background: url(../assets/bg.png) no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .title{
    font-size: 30px;
    padding-bottom: 20px;
  }
  .register-box{
    height: 350px;
    background-color: white;
    padding: 30px 50px;
    border-radius: 12px;
    min-width: 400px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .form-footer{
      display: flex;
      justify-content: center;
     .el-button--primary{
      width: 160px;
      height: 40px;
      border-radius: 20px;
     }
    }
  }
}
</style>